const http = require('./utils/http.js')
const Api = require('./utils/api.js')

const config = require('./assets/js/config')

App({
	onLaunch() {
		// this.getUserInfo()
		// 检查token
		if (wx.getStorageSync('token')) {
			// 检查用户信息
			if (!wx.getStorageSync('userInfo')) {
				// 检查登录态
				wx.checkSession({
					success: _ => { this.setUserInfo() },
					fail: err => { this.login() }
				})
			}
		} else {
			// token不存在
			this.login()
		}
	},


	login() {
		wx.login({
			success: res => {
				this.post(Api.user.login, {
					code: res.code
				}).then(res => {
					let { token } = res.data
					wx.setStorageSync('token', token)
					// this.setUserInfo().then(userInfo => {
					// 	this.regist(userInfo, token)
					// 	console.log(userInfo)
					// })
					typeof this.authCallback === 'function' && this.authCallback(token)
				})
			}
		})
	},

	// 缓存用户信息
	setUserInfo() {
		return new Promise(resolve => {
			wx.getUserInfo({
				withCredentials: true,
				success: res => {
					wx.setStorageSync('userInfo', res.userInfo)
					resolve(res.userInfo)
				},
				// 拒绝授权
				fail: err => {
					wx.showModal({
						title: '糟糕',
						content: '若不授权获取微信个人信息，则无法使用本小程序部分功能；点击同意授权，进入设置开启【用户信息】选项，则可重新使用；若您不愿开启，后期还想使用本小程序，您可以在微信【发现】->【小程序】->长按【快修猿】->删除，然后重新搜索打开，点击【授权】，可继续使用',
						showCancel: false,
						confirmText: '同意授权',
						success: res => {
							res.confirm && this.reAuth()
						}
					})
				}
			})
		})
	},

	// 重新发起授权
	reAuth() {
		wx.openSetting({
			success: res => {
				if (res.authSetting["scope.userInfo"]) {
					this.setUserInfo().then(userInfo => {
						this.regist(userInfo, wx.getStorageSync('token'))
					})
				}
			}
		})
	},


	// 提交服务端
	regist(userInfo, token) {
		this.post(Api.user.update, {
			'token': token,
			nickname: userInfo.nickName,
			avatar: userInfo.avatarUrl
		})
	},

	getUserInfo() {
		return new Promise(resolve => {
			if (this.globalData.userInfo) {
				resolve(this.globalData.userInfo)
			} else {
				wx.getUserInfo({
					withCredentials: true,
					success: res => {
						this.globalData.userInfo = res.userInfo
						resolve(res.userInfo)
					},
					// 拒绝授权
					fail: err => {
						wx.showModal({
							title: '糟糕',
							content: '若不授权获取微信个人信息，则无法使用本小程序部分功能；点击同意授权，进入设置开启【用户信息】选项，则可正常使用',
							showCancel: false,
							confirmText: '同意授权',
							success: res => {
								res.confirm && wx.openSetting({
									success: res => {
										if (res.authSetting["scope.userInfo"]) {
											this.setUserInfo().then(userInfo => {
												this.globalData.userInfo = res.userInfo
												resolve(userInfo)
											})
										}
									}
								})
							}
						})
					}
				})
			}
		})
	},

	// 在app上注册 request方法
	...http,

	// 全局变量
	globalData: {
      config, // 配置文件
      Api, // 接口文件
      userInfo: null,
		APP_NAME: 'HPU迎新'
	}
})
getApp().__proto__.test = () => 1